package com.gitee.simons.common.config;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * @author jsm
 * @date 2019/5/15 09:53
 * @Description 修改springboot临时文件路径
 */
@Configuration
public class UpLoadDirConfig {

    @Value("${spring.application.name}")
    private String applicationName;

    @Bean
    MultipartConfigElement multipartConfigElement()throws Exception {
        // 创建临时文件路径
        String location = System.getProperty("user.dir") + System.getProperty("file.separator") + "home" + System.getProperty("file.separator") +
                "tmp" + System.getProperty("file.separator") + applicationName;
        FileUtils.forceMkdir(new File(location));

        // 重启时清楚临时文件路径
        FileUtils.cleanDirectory(new File(location));
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}
